﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式.Command
{
    // "Command"
    public abstract class Command
    {
        // Fields
        protected Receiver receiver;

        // Constructors
        public Command(Receiver receiver)
        {
            this.receiver = receiver;
        }

        // Methods
        abstract public void Execute();
    }

    // "ConcreteCommand"
    public class ConcreteCommand : Command
    {
        // Constructors
        public ConcreteCommand(Receiver receiver) :
            base(receiver) { }

        // Methods
        public override void Execute()
        {
            receiver.Action();
        }
    }

    // "Receiver"
    public class Receiver
    {
        // Methods
        public void Action()
        {
            Console.WriteLine("Called Receiver.Action()");
        }
    }

    // "Invoker"
    public class Invoker
    {
        // Fields
        private Command command;

        // Methods
        public void SetCommand(Command command)
        {
            this.command = command;
        }

        public void ExecuteCommand()
        {
            command.Execute();
        }
    } 
}
